package com.flower.subject.api.completable;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-09-24 10:47
 */
public class CompletableFutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture future1 = CompletableFuture.runAsync(()->{
            System.out.println("没有返回值的");
        });

        CompletableFuture future = CompletableFuture.supplyAsync(()->{
            return "执行有返回值的任务";
        });

        System.out.println(future.get());
    }

    public void Task() throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(2000);
                return "futureTask1执行完成";
            }
        });

        new Thread(futureTask).start();
        System.out.println(futureTask.get());
        System.out.println("TODO");
    }

}
